package com.shop2cn.iapi.promotion.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import net.sf.json.JSONObject;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.noggit.JSONParser;

import com.google.common.collect.Lists;
import com.shop2cn.iapi.promotion.parameter.getProductsPromotionValueBean;
import com.shop2cn.iapi.promotion.serivce.getProductsPromotionValueCall;
import com.shop2cn.iapi.sellerpromotion.parameter.CreatePromotionBean;
import com.shop2cn.iapi.sellerpromotion.parameter.PromotionItem;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.Shop2cnSellerPromotionCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.utils.TestDataManager;
/**
*获取商品的有效促销活动
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getProductsPromotionValue{
	private static getProductsPromotionValueBean bean;
	private static getProductsPromotionValueCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商品的有效促销活动");
	}

	@Before
	public void caseUp() {
		bean=new getProductsPromotionValueBean();
		call=new getProductsPromotionValueCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProductsPromotionValue_001() {
		Logger.start(true,"获取商品的有效促销活动-满减-返回成功");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			String pid =TestDataManager.getRandomUUID().toString();
			String pid2 =TestDataManager.getRandomUUID().toString();
			String pid3 =TestDataManager.getRandomUUID().toString();
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(mchId, 1,Arrays.asList(new PromotionItem(1, 2, 1d)));
			createpromotionBean.setProductIdList(Arrays.asList(pid,pid2,pid3));
			
			createpromotionBean.setType(1);
			List<PromotionItem> promotionItemList=new ArrayList<PromotionItem>();
			PromotionItem e=new PromotionItem();
			e.setLevel(1);
			e.setUseCondition(BigDecimal.valueOf(10));
			e.setValue(BigDecimal.valueOf(1));
			promotionItemList.add(e);
			
			PromotionItem e2=new PromotionItem();
			e2.setLevel(2);
			e2.setUseCondition(BigDecimal.valueOf(20));
			e2.setValue(BigDecimal.valueOf(2));
			promotionItemList.add(e2);
			
			createpromotionBean.setPromotionItemList(promotionItemList);
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			
			bean.setSellerId(mchId);
			bean.setProductIdList(Lists.newArrayList(pid,pid2,pid3));
			call.setData(bean);
			call.callService();			
			
			org.json.JSONObject return_obj=new org.json.JSONObject(new org.json.JSONObject(call.getReturnData()).getJSONObject("Data").getJSONArray("list").get(0).toString());
			Logger.verifyEquals("200", call.getCode(), "验证返回Code");
			Logger.verifyEquals(e2.getValue(),new BigDecimal(return_obj.getDouble("promotionValue")), "验证返回promotionValue");
			Logger.verifyEquals(false,return_obj.getBoolean("discount"), "验证返回discount");
			Logger.verifyEquals(true,return_obj.getBoolean("multiLevel"), "验证返回multiLevel");
			for(int i=0;i<return_obj.getJSONArray("productIdList").length();i++)
			{
			  Logger.verifyEquals(true,bean.getProductIdList().contains(return_obj.getJSONArray("productIdList").get(i)), "验证返回productIdList");
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProductsPromotionValue_002() {
		Logger.start(true,"获取商品的有效促销活动-满减-没有层级关系-返回成功");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			String pid =TestDataManager.getRandomUUID().toString();
			String pid2 =TestDataManager.getRandomUUID().toString();
			String pid3 =TestDataManager.getRandomUUID().toString();
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(mchId, 1,Arrays.asList(new PromotionItem(1, 2, 1d)));
			createpromotionBean.setProductIdList(Arrays.asList(pid,pid2,pid3));
			
			createpromotionBean.setType(1);
			List<PromotionItem> promotionItemList=new ArrayList<PromotionItem>();
			PromotionItem e1=new PromotionItem();
			e1.setLevel(1);
			e1.setUseCondition(BigDecimal.valueOf(10));
			e1.setValue(BigDecimal.valueOf(1));
			promotionItemList.add(e1);
			
			createpromotionBean.setPromotionItemList(promotionItemList);
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			
			bean.setSellerId(mchId);
			bean.setProductIdList(Lists.newArrayList(pid,pid2,pid3));
			call.setData(bean);
			call.callService();			
			
			org.json.JSONObject return_obj=new org.json.JSONObject(new org.json.JSONObject(call.getReturnData()).getJSONObject("Data").getJSONArray("list").get(0).toString());
			Logger.verifyEquals("200", call.getCode(), "验证返回Code");
			Logger.verifyEquals(e1.getValue(),new BigDecimal(return_obj.getDouble("promotionValue")), "验证返回promotionValue");
			Logger.verifyEquals(false,return_obj.getBoolean("discount"), "验证返回discount");
			Logger.verifyEquals(false,return_obj.getBoolean("multiLevel"), "验证返回multiLevel");
			for(int i=0;i<return_obj.getJSONArray("productIdList").length();i++)
			{
			  Logger.verifyEquals(true,bean.getProductIdList().contains(return_obj.getJSONArray("productIdList").get(i)), "验证返回productIdList");
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProductsPromotionValue_003() {
		Logger.start(true,"获取商品的有效促销活动-折扣-返回成功");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			String pid =TestDataManager.getRandomUUID().toString();
			String pid2 =TestDataManager.getRandomUUID().toString();
			String pid3 =TestDataManager.getRandomUUID().toString();
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(mchId, 1,Arrays.asList(new PromotionItem(1, 2, 1d)));
			createpromotionBean.setProductIdList(Arrays.asList(pid,pid2,pid3));
			
			createpromotionBean.setType(2);
			List<PromotionItem> promotionItemList=new ArrayList<PromotionItem>();
			PromotionItem e1=new PromotionItem();
			e1.setLevel(1);
			e1.setUseCondition(BigDecimal.valueOf(10));
			e1.setValue(BigDecimal.valueOf(1));
			promotionItemList.add(e1);
			
			PromotionItem e2=new PromotionItem();
			e2.setLevel(2);
			e2.setUseCondition(BigDecimal.valueOf(20));
			e2.setValue(BigDecimal.valueOf(2));
			promotionItemList.add(e2);
			
			createpromotionBean.setPromotionItemList(promotionItemList);
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			
			bean.setSellerId(mchId);
			bean.setProductIdList(Lists.newArrayList(pid,pid2,pid3));
			call.setData(bean);
			call.callService();			
			
			
			org.json.JSONObject return_obj=new org.json.JSONObject(new org.json.JSONObject(call.getReturnData()).getJSONObject("Data").getJSONArray("list").get(0).toString());
			Logger.verifyEquals("200", call.getCode(), "验证返回Code");
			Logger.verifyEquals(e1.getValue().divide(new BigDecimal(10)),new BigDecimal(return_obj.getString("promotionValue")), "验证返回promotionValue");
			Logger.verifyEquals(true,return_obj.getBoolean("discount"), "验证返回discount");
			Logger.verifyEquals(true,return_obj.getBoolean("multiLevel"), "验证返回multiLevel");
			for(int i=0;i<return_obj.getJSONArray("productIdList").length();i++)
			{
			  Logger.verifyEquals(true,bean.getProductIdList().contains(return_obj.getJSONArray("productIdList").get(i)), "验证返回productIdList");
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}